package com.megvii.koala.components;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.megvii.corelib.util.MegviiToast;
import com.megvii.koala.R;

/**
 * Created by linyue on 15/12/28.
 */
public class LimitEditText extends RelativeLayout
{
    public static final int DEFAULT_LIMITATION = 30;

    protected EditText editText;

    protected TextView numText;

    protected int limitation = DEFAULT_LIMITATION;

    public LimitEditText(Context context)
    {
        super(context);
        init(context, null);
    }

    public LimitEditText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context, attrs);
    }

    public LimitEditText(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs)
    {
        inflate(context, R.layout.limit_edit_layout, this);
        editText = (EditText) findViewById(R.id.limitedit_layout_edittext);
        numText = (TextView) findViewById(R.id.limitedit_layout_numtext);
        if (attrs != null)
        {
            TypedArray array = getContext()
                    .obtainStyledAttributes(attrs, R.styleable.LimitEditText);
            setLimitation(array.getInt(R.styleable.LimitEditText_limitation, DEFAULT_LIMITATION));
            setSingleLine(array.getBoolean(R.styleable.LimitEditText_singleLine, false));
        }
        editText.addTextChangedListener(new EditChangedListener());
    }

    public int getLimitation()
    {
        return limitation;
    }

    public void setLimitation(int limitation)
    {
        this.limitation = limitation;
    }

    public void setSingleLine(boolean singleLine)
    {
        editText.setSingleLine(singleLine);
    }

    public void setText(String text, boolean selectAll)
    {
        if (text != null)
        {
            int num = text.length();
            if (num > limitation)
            {
                text = text.substring(0, limitation);
            }
            editText.setText(text);
            if (selectAll)
            {
                editText.selectAll();
            }
            //TODO
            numText.setText(String.valueOf(limitation - text.length()));
        }
    }

    public String getText()
    {
        return editText.getText().toString();
    }

    class EditChangedListener implements TextWatcher
    {
        private CharSequence temp;// 监听前的文本

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after)
        {
            temp = s;
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count)
        {
            numText.setText((limitation - s.length()) + "");
        }

        @Override
        public void afterTextChanged(Editable s)
        {
            /** 得到光标开始和结束位置 ,超过最大数后记录刚超出的数字索引进行控制 */
            int editStart = editText.getSelectionStart();
            int editEnd = editText.getSelectionEnd();
            int editLength = temp.length();
            if (editLength > limitation)
            {
                MegviiToast.showLongToast(getContext(), "你输入的字数已经超过了限制！");
                s.delete(editStart - 1, editEnd);
                int tempSelection = editStart - 1;
                editText.setText(s);
                editText.setSelection(tempSelection); // 设置光标在最尾端
            }
        }
    }
}
